//
//  SISSContractChooseCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/16.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractChooseCell.h"
#import "SISSPickerView.h"
#import "SISSDatePickerView.h"

@interface SISSContractChooseCell ()<SISSDatePickerViewDelegate,UITextFieldDelegate>

/* leftLabel */
@property (weak, nonatomic) UILabel *leftLabel;
/* rightTextField */
@property (weak, nonatomic) UITextField *rightTextField;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

/// tapGesture
@property (weak, nonatomic) UITapGestureRecognizer *tapGesture;

@end
@implementation SISSContractChooseCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    if (contractModel.isDate) {
        self.rightTextField.text = contractModel.rightStr && contractModel.rightStr.length >= 10 ? [contractModel.rightStr substringToIndex:10] : contractModel.rightStr;
    } else {
        self.rightTextField.text = contractModel.rightStr;
    }
    self.bottomLine.hidden = contractModel.isHiddenBottomLine;
    CGSize size = [contractModel.leftStr boundingRectWithSize:CGSizeMake(600, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
    
    CGFloat offset = size.width > 150 ? size.width + 16 : 160;
   
    WeakSelf
    [self.rightTextField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(offset);
    }];
    
    if ([[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.leftLabel.attributedText = attributedStr;
    } else {
        self.leftLabel.text = contractModel.leftStr;
    }
    self.rightTextField.placeholder = contractModel.placeHolder;
    
    if (contractModel.isClickJump) {
        self.rightTextField.textAlignment = NSTextAlignmentRight;
        [self removeGestureRecognizer:self.tapGesture];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.contractModel.rightStr = textField.text;
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = MAIN_TEXT_COLOR;
    leftLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:leftLabel];
    self.leftLabel = leftLabel;
    
    UITextField *rightTextField = [[UITextField alloc] init];
    rightTextField.font = [UIFont systemFontOfSize:16];
    rightTextField.userInteractionEnabled = NO;
    rightTextField.textColor = MAIN_TEXT_COLOR;
    rightTextField.delegate = self;
    rightTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    [self.contentView addSubview:rightTextField];
    self.rightTextField = rightTextField;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_list_more"]];
    [self addSubview:imageView];
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    // 自动布局
    CGFloat margin = 8.0;
    WeakSelf
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
    }];
    
    [self.rightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(20 * margin);
        make.right.equalTo(imageView.mas_left).offset(-2 * margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView);
        make.centerY.equalTo(weakSelf.contentView);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.top.equalTo(weakSelf.leftLabel.mas_bottom).offset(1.5 * margin);
        make.height.mas_equalTo(1);
    }];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickAction)];
    [self addGestureRecognizer:tapGesture];
    self.tapGesture = tapGesture;

}

- (void)tapClickAction {
    [[[self.contentView superview] superview] endEditing:YES];
    
    UIResponder *responder = self;
    UIViewController *viewController;
    while ((responder = [responder nextResponder])){
        if ([responder isKindOfClass: [UIViewController class]]) {
            viewController = (UIViewController *)responder;
            break;
        }
    }
    [viewController.view endEditing:YES];
    
    if (self.contractModel.isDate) {
        SISSDatePickerView *datePickerView = [[SISSDatePickerView alloc] initWithType:UIDatePickerModeDate title:[self.contractModel.leftStr substringFromIndex:1]];
        NSDate *date = self.contractModel.rightStr.length > 0 ? [NSDate yyr_stringToDate:self.contractModel.rightStr withDateFormat:@"yyyy-MM-dd HH:mm:ss"] : [NSDate date];
        [datePickerView setNowTime:date ? date : [NSDate date]];
        if ([self.contractModel.leftStr containsString:@"委托开始时间"] || [self.contractModel.leftStr containsString:@"委托结束时间"]) {
            datePickerView.minimumDate = [NSDate date];
        }
        datePickerView.delegate = self;
        
    } else {
        
        SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        NSInteger length = 0;
        NSInteger location = 0;
        if ([self.contractModel.leftStr hasPrefix:@"*"]) {
            length += 1;
            location = 1;
        }
        if ([self.contractModel.leftStr hasSuffix:@"："]) {
            length += 1;
        }
        
        pickerView.title = [self.contractModel.leftStr substringWithRange:NSMakeRange(location, self.contractModel.leftStr.length - length)];
        BOOL isArea = self.contractModel.isArea;
        pickerView.isArea = isArea;
        pickerView.titleArray = self.contractModel.chooseArr;
            pickerView.firstIndex = self.contractModel.firstIndex;
        //    __weak typeof(self) weakself = self;
        pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
            if (isArea) {
                self.rightTextField.text = [NSString stringWithFormat:@"%@-%@",firstTitle,secondTitle];
                self.contractModel.rightStr = [NSString stringWithFormat:@"%@-%@",firstTitle,secondTitle];
                self.contractModel.region = firstTitle;
                self.contractModel.regionDetail = secondTitle;
                self.contractModel.secondIndex = secondIndex;
            } else {
                self.rightTextField.text = firstTitle;
                self.contractModel.rightStr = firstTitle;
                if ([self.delegate respondsToSelector:@selector(contractChooseCell:chooseIndex:)]) {
                    [self.delegate contractChooseCell:self chooseIndex:firstIndex];
                }
            }
            self.contractModel.firstIndex = firstIndex;
            
        };
        
        [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
    }
    
    
}

#pragma mark -- SISSDatePickerViewDelegate 选择时间后赋值
- (void)determine:(SISSDatePickerView *)choose date:(NSDate *)date {
    
    NSString *timeStr = [NSDate yyr_dateToString:date withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"determine  %@",timeStr);
    self.rightTextField.text = [timeStr substringToIndex:10];
    self.contractModel.rightStr = timeStr;
//    if (choose.tag == 100) {
//        self.startTimeView.rightStr = [timeStr substringToIndex:10];
//        self.startTime = timeStr;
//        self.startDate = date;
//        if (self.endTime) return;
//        [self clickChooseActionWithChooseView:self.endTimeView];
//    } else {
//        self.endTimeView.rightStr = [timeStr substringToIndex:10];
//        self.endTime = timeStr;
//        self.endDate = date;
//        if (self.commissionRateStr) return;
//        [self clickChooseActionWithChooseView:self.ownerCommissionView];
//    }
}

- (void)changeTime:(SISSDatePickerView *)choose date:(NSDate *)date {
    
    NSString *timeStr = [choose stringFromDate:date];
    NSLog(@"changeTime  %@",timeStr);
}



@end
